package cn.rongdao.jrkabao.ui.takephoto.uicomponent;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.content.ContextCompat;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.mifengkong.frtools.util.FRCollection;
import com.mifengkong.frtools.util.FRImageLoad;
import com.mifengkong.frtools.util.FRString;
import com.mifengkong.frwidget.PercentLinearLayout;

import cn.rongdao.jrkabao.R;

/**
 * Created by jiangyongxing on 2017/6/23.
 * 描述：
 */

public class TakePhotoComponent extends PercentLinearLayout {

    private ImageView mPicIv;
    private TextView mNameTv;

    public TakePhotoComponent(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mPicIv = findViewById(R.id.lctp_pic);
        mNameTv = findViewById(R.id.lctp_name);
    }

    public void setNameAndDesc(String name, String desc) {
        if (FRString.isEmpty(name, desc))
            return;
        String content = FRCollection.appendAsString(name, "\n", desc, "");
        SpannableString ss = new SpannableString(content);
        ss.setSpan(new ForegroundColorSpan(ContextCompat.getColor(getContext(), R.color.c999999)), name.length(), content.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        ss.setSpan(new AbsoluteSizeSpan(12,true), name.length(), content.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        mNameTv.setText(ss);
    }

    public void setPhoto2IV(String path) {
        setPhoto2IV(path, 0f);
    }

    public void setPhoto2IV(String path, float rotate) {
        FRImageLoad.loadImageTransForm(path, mPicIv, rotate);
    }

    public void setPhoto2IV(Bitmap bm){
        mPicIv.setImageBitmap(bm);
    }

    public void setPhoto2IV(int resId) {
        if (resId == 0)
            return;
        mPicIv.setImageResource(resId);
    }

    public void removeIVPhoto() {
        mPicIv.setImageDrawable(null);
    }

    public void setOnClickListener(View.OnClickListener onClickListener) {
        mPicIv.setOnClickListener(onClickListener);
    }
}
